//  ForgetSecretView.m
//  XJMTechnician
//  Created by Mr.hu on 2017/10/17.
//  Copyright © 2017年 Mr.hu. All rights reserved.

#import "AccountRegister.h"
#import "IdentityReigster.h"
#import "Xijiaome_protolVC.h"

@interface AccountRegister ()<UITextFieldDelegate>
{
    NSInteger _runLoopTimes;
    NSTimer * unregisteredTimer;
}
@property (weak, nonatomic) IBOutlet UIButton *ForgetUpdateSubmitBtn;

@property (weak, nonatomic) IBOutlet UITextField *InputPhotoNumber;
@property (weak, nonatomic) IBOutlet UITextField *VerificationCode;
@property (weak, nonatomic) IBOutlet UITextField *InputNewSecret;
@property (weak, nonatomic) IBOutlet UITextField *InputSureSecret;

@property (weak, nonatomic) IBOutlet UIButton *VerificationReceiveBtn;

@property (weak, nonatomic) IBOutlet UIView *FirstForgetView;
@property (weak, nonatomic) IBOutlet UIView *SecondForgetView;
@property (weak, nonatomic) IBOutlet UIView *ThreeForgetView;
@property (weak, nonatomic) IBOutlet UIView *FourForgetView;

@property (nonatomic,copy) NSString * codeID;

@end

@implementation AccountRegister

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户注册";
    
    self.codeID = [[NSString alloc]init];
    self.InputPhotoNumber.delegate = self;
    self.VerificationCode.delegate = self;
    self.InputNewSecret.delegate = self;
    self.InputSureSecret.delegate = self;
    
    self.VerificationReceiveBtn.layer.cornerRadius  = 15;
    self.VerificationReceiveBtn.layer.masksToBounds = YES;
    self.ForgetUpdateSubmitBtn.layer.cornerRadius  = 15;
    self.ForgetUpdateSubmitBtn.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jkr_tapGestureAction:)];
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
}

- (IBAction)CheckAccount_SelectMeth:(UIButton *)sender {
    
    if (sender.selected == NO) {
        [sender setImage:[UIImage imageNamed:@"weixuanze"] forState:UIControlStateNormal];
        sender.selected = YES;
        self.ForgetUpdateSubmitBtn.backgroundColor = [UIColor lightGrayColor];
        self.ForgetUpdateSubmitBtn.userInteractionEnabled = NO;
    }else{
        [sender setImage:[UIImage imageNamed:@"xuanze"] forState:UIControlStateNormal];
        sender.selected = NO;
        self.ForgetUpdateSubmitBtn.backgroundColor = kthemeColor;
        self.ForgetUpdateSubmitBtn.userInteractionEnabled = YES;
    }
}
- (IBAction)xijiaome_ProtoclMeth:(id)sender {
    
    Xijiaome_protolVC * xijiao = [[Xijiaome_protolVC alloc]init];
    [self.navigationController pushViewController:xijiao animated:YES];
}


- (void)jkr_tapGestureAction:(UITapGestureRecognizer *)tapGusture{
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.InputPhotoNumber resignFirstResponder];
    [self.VerificationCode resignFirstResponder];
    [self.InputNewSecret resignFirstResponder];
    [self.InputSureSecret resignFirstResponder];
    return NO;
}

- (IBAction)ClearPhoneNumber:(id)sender {
    
    self.InputPhotoNumber.text = nil;
    
}

- (IBAction)ReceiveVerificationCodeMeth:(id)sender {
    
    if (self.InputPhotoNumber.text.length < 11) {
        [XJToolClass tipAnimationWith:@"请输入手机号"];
        return;
    }
    
    
    if (![XJToolClass checkTel:self.InputPhotoNumber.text]) {
        [XJToolClass tipAnimationWith:@"请输入正确的手机号码"];
    }else{
    
        _runLoopTimes = 0;
        self.VerificationReceiveBtn.userInteractionEnabled = NO;
        if(unregisteredTimer == nil) {
            unregisteredTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(runLoop) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:unregisteredTimer forMode:NSRunLoopCommonModes];
        }
        
        //1技师注册发送，2技师找回密码发送
        [XJHttpClass Sendmessagenumber:self.InputPhotoNumber.text messagetype:@"1" andFinishblock:^(id response, NSString *errStr) {
            if (errStr) {
                [XJToolClass timeChangeint:@"短信验证失败"];
            }else{
                NSDictionary * datadict = response[@"data"];
                self.codeID =  [NSString stringWithFormat:@"%@",datadict[@"code_id"]];
            }
        }];
    }
}

- (IBAction)SureChangeSecretMeth:(id)sender {
    
    //字母和数字的判断
    if (self.InputNewSecret.text.length >=6) {
        
        if ([self.InputNewSecret.text isEqualToString:self.InputSureSecret.text]) {
            
            [XJHttpClass TeachRegisteraccount:self.InputPhotoNumber.text Pwd:self.InputNewSecret.text restpwd:self.InputSureSecret.text code_id:self.codeID code:self.VerificationCode.text version:CurrentVersion source:@"ios" andFinishblock:^(id response, NSString *errStr) {
                if (errStr) {
                    [XJToolClass tipAnimationWith:[NSString stringWithFormat:@"%@",errStr]];
                }else{
                    NSDictionary * datadic = response[@"data"];
                    if ([datadic isKindOfClass:[NSDictionary class]]) {
                        [TechSimple registerpass].User_TechID = [NSString stringWithFormat:@"%ld",[datadic[@"tid"] integerValue]];
                        [TechSimple registerpass].User_TechPhone =  datadic[@"mobile"];
                        IdentityReigster * identy = [[IdentityReigster alloc]init];
                        [self.navigationController pushViewController:identy animated:false];
                    }
                }
            }];
        }else{
            [XJToolClass tipAnimationWith:@"密码输入错误!"];
        }
    }else{
        [XJToolClass tipAnimationWith:@"请输入六位以上的密码"];
    }
}

// 倒计时
- (void)runLoop
{
    _runLoopTimes++;
    [self.VerificationReceiveBtn setTitle:[NSString stringWithFormat:@"重新发送(%@)", @(60-_runLoopTimes)] forState:UIControlStateNormal];
    
    if (_runLoopTimes == 60)
    {
        [unregisteredTimer invalidate];
        unregisteredTimer = nil;
        self.VerificationReceiveBtn.userInteractionEnabled = YES;
        _runLoopTimes = 0;
        [self.VerificationReceiveBtn setTitle:@"重新发送" forState:UIControlStateNormal];
    }
}

@end
